package com.qudian.framework.dao.typeHandler;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import com.qudian.framework.dao.base.Money;

public class MoneyTypeHandler implements TypeHandler<Object> {
	
	@Override
	public Object getResult(ResultSet arg0, String arg1) throws SQLException {
		Money money = new Money();
		money.setCent(arg0.getLong(arg1));
		return money;
	}
	
	@Override
	public Object getResult(CallableStatement arg0, int arg1) throws SQLException {
		Money money = new Money();
		money.setCent(arg0.getLong(arg1));
		return money;
	}
	
	@Override
	public void setParameter(PreparedStatement arg0, int arg1, Object arg2, JdbcType arg3) throws SQLException {
		if (null != arg2) {
			Money money = (Money) arg2;
			arg0.setBigDecimal(arg1, new BigDecimal(money.getCent()));
		} else {
			arg0.setBigDecimal(arg1, null);
		}
	}
	
	@Override
	public Object getResult(ResultSet arg0, int arg1) throws SQLException {
		Money money = new Money();
		money.setCent(arg0.getLong(arg1));
		return money;
	}
}
